package com.java.dao.impl;

import com.java.bean.Student;
import com.java.dao.StudentDao;
import com.java.utils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wjr
 * @Description:
 * @Date Created in  2020-09-04 15:55
 * @Modified By:
 */
public class StudenDaotImpl extends DruidUtil implements StudentDao{
    @Override
    public List<Student> getAll() {
        List list = new ArrayList();
        Connection coon = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            coon = getConnection();
            String sql = "select * from student";
            preparedStatement = coon.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            //获取数据库中每一行的数据，并添加到list中
            while(rs.next()){
                Student student = new Student();
                student.setStudentId(rs.getInt("studentid"));
                student.setStuName(rs.getString("stuname"));
                student.setStudentNo(rs.getString("studentNo"));
                student.setStuAge(rs.getInt("stuage"));
                student.setGradeId(rs.getInt("gradeid"));
                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭连接
            close(coon,preparedStatement,rs);
        }
        return list;
    }

    @Override
    public boolean deleteStudent(Student student) {
        if (student != null){
            //连接对象
            Connection conn = null;
            PreparedStatement preparedStatement = null;
            ResultSet rs = null;
            //返回操作结果状态码，0代表没有操作成功
            int execute = -1;
            String sql = "delete from student where studentid=?";
            try {
                conn = getConnection();
                preparedStatement = conn.prepareStatement(sql);
                preparedStatement.setInt(1,student.getStudentId());
                execute = preparedStatement.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                close(conn,preparedStatement);
                if(execute >= 1){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Student selectStudentById(int Id) {
        Student student = new Student();
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;

        String sql = "select * from student where studentid = " + Id;
        try {
            conn = getConnection();
            preparedStatement = conn.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            while (rs.next()){
                student.setStudentId(rs.getInt("studentid"));
                student.setStuName(rs.getString("stuname"));
                student.setStudentNo(rs.getString("studentNo"));
                student.setStuAge(rs.getInt("stuage"));
                student.setGradeId(rs.getInt("gradeid"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(conn,preparedStatement,rs);
        }
        return student;
    }

    @Override
    public boolean addStudent(Student student) {
        Connection conn = null;
        PreparedStatement statement = null;
        String sql = "insert into student values(null,?,?,?,?)";
        //返回操作结果状态码，0代表没有操作成功
        int execute = -1;
        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);
            statement.setString(1,student.getStudentNo());
            statement.setString(2,student.getStuName());
            statement.setInt(3,student.getStuAge());
            statement.setInt(4,student.getGradeId());
            execute = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(conn,statement);
            if (execute == 1){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean updateStudent(Student student) {
        Connection conn = null;
        PreparedStatement statement = null;
        int execute = -1;
        String sql = "update student set studentno = ?,stuname=?,stuage=?,gradeid=? where studentid=?";
        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);
            statement.setString(1,student.getStudentNo());
            statement.setString(2,student.getStuName());
            statement.setInt(3,student.getStuAge());
            statement.setInt(4,student.getGradeId());
            statement.setInt(5,student.getStudentId());
            execute = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(conn,statement);
            if (execute == 1){
                return true;
            }
        }
        return false;
    }
}
